package com.wenlei.servlet;

import com.wenlei.InitWebApplicationInitializer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;

/**
 * 路径是/finalInfo
 * @see InitWebApplicationInitializer 中设置
 * @author  wenlei
 */
public class FinalServlet extends HttpServlet {

    @Override
    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter writer = response.getWriter();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append( "<table border=1px>" );
        stringBuilder.append( "<tr><th colspan=2 >This is final Servlet Info is: <th><tr>"  );
        stringBuilder.append( "<tr><td>javax.servlet.include.request_uri</td><td>" ).append( request.getRequestURI() ).append( "</td></tr>" );
        stringBuilder.append("<tr><td>javax.servlet.include.context_path</td><td>" ).append( request.getContextPath() ).append( "</td></tr>" );
        stringBuilder.append("<tr><td>javax.servlet.include.servlet_path</td><td>" ).append( request.getServletPath() ).append( "</td></tr>" );
        stringBuilder.append("<tr><td>javax.servlet.include.path_info</td><td>" ).append( request.getPathInfo() ).append( "</td></tr>" );
        stringBuilder.append("<tr><td>javax.servlet.include.query_string</td><td>" ).append( request.getQueryString() ).append( "</td></tr>" );
        stringBuilder.append( "</table>" );
        println(writer, stringBuilder.toString());
    }

    private static void println(Writer writer, Object  message) throws IOException {
        writer.write(String.format("[线程 - %s] : %s\n", Thread.currentThread().getName(), message));
        writer.flush();
    }

}
